package do

import (
	"fmt"
	"time"

	"gorm.io/gorm"
)

// / 分类关联表
type Category struct {
	ID      int `gorm:"primarykey"`
	ClassID int `gorm:"column:class_id;NOT NULL;uniqueIndex:idx_class_level_brand"` // 类目
	LevelID int `gorm:"column:level_id;NOT NULL;uniqueIndex:idx_class_level_brand"` // 等级
	BrandID int `gorm:"column:brand_id;NOT NULL;uniqueIndex:idx_class_level_brand"` // 品牌

	CreatedAt time.Time `gorm:"autoCreateTime"`
	UpdatedAt time.Time `gorm:"autoUpdateTime"`

	// 用于传递数据
	ClassName string `gorm:"-"`
	LevelName string `gorm:"-"`
	BrandName string `gorm:"-"`
}

func (*Category) TableName() string {
	return "category"
}

func (c *Category) BeforeSave(tx *gorm.DB) (err error) {
	if c.ClassID <= 0 {
		return fmt.Errorf("类目id不能为空")
	}
	if c.LevelID <= 0 {
		return fmt.Errorf("等级id不能为空")
	}
	if c.BrandID <= 0 {
		return fmt.Errorf("品牌id不能为空")
	}
	return nil
}
